package com.timoon.manager.service;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import org.springframework.stereotype.Service;

import com.timoon.app.resource.image.ImageLocation;
import com.timoon.app.utils.GlobalRandom;
import com.timoon.manager.panel.product.ProductGridService;
import com.timoon.manager.panel.product.ProductSortService;
import com.timoon.manager.panel.product.ProductTypeService;
import com.timoon.manager.panel.product.cell.ProductGridCellService;
import com.timoon.sample.dao.ProductDAO;
import com.timoon.sample.dao.ProductInstructionPicDAO;
import com.timoon.sample.dao.ProductPropertyDAO;
import com.timoon.sample.dao.ProductSortDAO;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductExample;
import com.timoon.sample.dao.model.ProductInstructionPic;
import com.timoon.sample.dao.model.ProductInstructionPicExample;
import com.timoon.sample.dao.model.ProductProperty;
import com.timoon.sample.dao.model.ProductPropertyExample;
import com.timoon.sample.dao.model.ProductSort;
import com.timoon.sample.dao.model.ProductSortExample;
import com.timoon.sample.utils.SystemConfig;

@Service(value="manager.product")
public class ProductService implements ProductGridService,ProductSortService,ProductGridCellService ,ProductTypeService{

	@Resource
	private ProductDAO productDAOImpl;
	
	@Resource
	private ProductSortDAO productSortDAOImpl;
	
	@Resource
	private ProductInstructionPicDAO productInstructionPicDAOImpl;
	
	@Resource
	private ProductPropertyDAO productPropertyDAOImpl;
	
	@Override
	public List<Product> getAllProduct() {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByExample(new ProductExample(){{
			
			this.createCriteria().andStatusNotEqualTo(0);
		}});
	}

	@Override
	public ProductSort getSortByProductId(Integer id) {
		// TODO Auto-generated method stub
		return this.productSortDAOImpl.selectByPrimaryKey(id);
	}

	@Override
	public DefaultTreeModel getDefaultTreeModelWithProductSort() {
		// TODO Auto-generated method stub
		
		ProductSort rootSort=new ProductSort();
		rootSort.setName("根节点");
		DefaultMutableTreeNode root=new DefaultMutableTreeNode();
		root.setUserObject(rootSort);
		
		//获取所有父节点（parent_id=0）
		List<ProductSort> firstSort=this.productSortDAOImpl.selectByExample(new ProductSortExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andParentIdEqualTo(0);
		}});
		if(firstSort!=null){
			for(ProductSort s:firstSort){
				DefaultMutableTreeNode childNode=new DefaultMutableTreeNode(s);
				root.add(childNode);
				childNode=this.dealTreeNode(childNode);
			}
		}
		
		return new DefaultTreeModel(root);
	}
	
	private DefaultMutableTreeNode dealTreeNode(DefaultMutableTreeNode node){
		final ProductSort sort=(ProductSort) node.getUserObject();
		
		//获取子sort
		List<ProductSort> childSort=this.productSortDAOImpl.selectByExample(new ProductSortExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andParentIdEqualTo(sort.getId());
		}});
		
		if(childSort!=null&&childSort.size()>0){
		
			for(ProductSort s:childSort){
				DefaultMutableTreeNode childNode=new DefaultMutableTreeNode(s);
				node.add(childNode);
				childNode=this.dealTreeNode(childNode);
			}
		}
		return node;
	}

	@Override
	public List<ProductInstructionPic> getProductInstructionPicByProductId(
			final Integer id) {
		// TODO Auto-generated method stub
		return this.productInstructionPicDAOImpl.selectByExample(new ProductInstructionPicExample(){{
			this.createCriteria().andProductIdEqualTo(id);
		}});
	}

	@Override
	public void deleteProductInstructionPicById(Integer id) {
		// TODO Auto-generated method stub
		this.productInstructionPicDAOImpl.deleteByPrimaryKey(id);
	}

	@Override
	public void addProductInstructionPic(String path, Integer productId) {
		// TODO Auto-generated method stub
		ProductInstructionPic pic=new ProductInstructionPic();
		pic.setPath(path);
		pic.setProductId(productId);
		
		this.productInstructionPicDAOImpl.insert(pic);
	}

	@Override
	public List<ProductProperty> getProductPropertyByProductId(final Integer id) {
		// TODO Auto-generated method stub
		return this.productPropertyDAOImpl.selectByExample(new ProductPropertyExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andProductIdEqualTo(id);
		}});
	}

	@Override
	public void delProductPropertyById(Integer id) {
		// TODO Auto-generated method stub
		this.productPropertyDAOImpl.deleteByPrimaryKey(id);
	}

	@Override
	public void addProductProperty(ProductProperty productProperty) {
		// TODO Auto-generated method stub
		this.productPropertyDAOImpl.insert(productProperty);
	}

	@Override
	public void addProduct(Product product) {
		// TODO Auto-generated method stub
		if(product.getId()==null){
			this.productDAOImpl.insert(product);
		}else{
			this.productDAOImpl.updateByPrimaryKey(product);
		}
	}

	@Override
	public Product getRegistProduct() {
		// TODO Auto-generated method stub
		final Integer randomType=GlobalRandom.getRandom();
		Product product=new Product();
		product.setStatus(0);
		product.setActualPrice(Long.valueOf(0));
		product.setPhoto("localtion.png");
		product.setType(randomType);
		product.setName("default");
		this.productDAOImpl.insert(product);
		
		List<Product> ps=this.productDAOImpl.selectByExample(new ProductExample(){{
			this.createCriteria().andTypeEqualTo(randomType);
		}});
		
		return ps.get(0);
	}

	@Override
	public void deleteProductByIds(List<String> ids) {
		// TODO Auto-generated method stub
		for(String id:ids){
			Product product=this.productDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
			product.setStatus(SystemConfig.STATUS_DELETED);
			this.productDAOImpl.updateByPrimaryKey(product);
			
			//删除图片
			
			StringBuffer path=new StringBuffer(ImageLocation.class.getClassLoader().getResource("com/timoon/app/resource/image/").toString());
			
			//删除path中开头的"file:\"
			String starting=path.substring(6);
			
			File file=new File(starting+"/"+product.getPhoto());
			/*暂时不执行删除图片*/
//			if(file.exists()){
//				file.delete();
//			}else{
//				System.err.println("file不存在【"+file.getPath()+"】");
//			}
		}
	}

	@Override
	public void addProductSort(ProductSort sort) {
		// TODO Auto-generated method stub
		this.productSortDAOImpl.insert(sort);
	}

	@Override
	public void delProductSortById(final Integer id) {
		// TODO Auto-generated method stub
		ProductSort sort=this.productSortDAOImpl.selectByPrimaryKey(id);
		sort.setStatus(0);
		this.productSortDAOImpl.updateByPrimaryKey(sort);
		
		//删除下级
		List<ProductSort> childSorts=this.productSortDAOImpl.selectByExample(new ProductSortExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andParentIdEqualTo(id);
		}});
		
		for(ProductSort child:childSorts){
			delProductSortById(child.getId());
		}
	}

	@Override
	public List<Product> getProductsByType(final Integer type) {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByExample(new ProductExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andTypeEqualTo(type);
		}});
	}

	@Override
	public void updateProductTypeByIds(List<String> ids, Integer newType) {
		// TODO Auto-generated method stub
		
		for(String id:ids){
			Product product=this.productDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
			
			product.setType(newType);
			
			this.productDAOImpl.updateByPrimaryKey(product);
		}
	}

}
